package vn.wallet.app.helper;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.springframework.util.StringUtils;

public class PageUtils {

	private static final String INSERT_SUCCESS = AppUtils.getResource("wallet.app.insertSuccess");
	private static final String INSERT_FAILED = AppUtils.getResource("wallet.app.insertFailed");

	private static final String UPDATE_SUCCESS = AppUtils.getResource("wallet.app.updateSuccess");
	private static final String UPDATE_FAILED = AppUtils.getResource("wallet.app.updateFailed");

	private static final String DELETE_SUCCESS = AppUtils.getResource("wallet.app.deleteSuccess");
	private static final String DELETE_FAILED = AppUtils.getResource("wallet.app.deleteFailed");

	public static enum FUNC_NAME {
		VALIDATE, ADD, EDIT, DELETE
	}

	public static void logInfo(String compID, String strFuncName, String details) {
		RequestContext request = RequestContext.getCurrentInstance();

		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
				strFuncName, details);
		if (StringUtils.hasText(compID)) {
			FacesContext.getCurrentInstance().addMessage(null, msg);
		} else {
			FacesContext.getCurrentInstance().addMessage(compID, msg);
		}
		request.addCallbackParam("errorProcess", false);
		request.addCallbackParam("validationFailed", false);
	}

	public static void logSuccess(FUNC_NAME func_name, String details) {
		String strFuncName = "";

		switch (func_name) {
			case ADD:
				strFuncName = INSERT_SUCCESS;
				break;

			case EDIT:
				strFuncName = UPDATE_SUCCESS;
				break;

			default:
				strFuncName = DELETE_SUCCESS;
				break;
		}
		logInfo("", strFuncName, details);
	}

	public static void logSuccess(String compID, String details, String summary) {
		logInfo(compID, details, summary);
	}

	public static void logSuccess(String compID, String details) {
		logInfo(compID, details, "");
	}

	public static void logSuccess(String details) {
		logInfo("", "", details);
	}

	public static void logFailed(String compID, FUNC_NAME func_name,
			String details) {
		RequestContext request = RequestContext.getCurrentInstance();
		String strFuncName;

		switch (func_name) {
			case ADD:
				strFuncName = INSERT_FAILED;
				break;

			case EDIT:
				strFuncName = UPDATE_FAILED;
				break;

			default:
				strFuncName = DELETE_FAILED;
				break;
		}

		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				strFuncName, details);
		FacesContext.getCurrentInstance().addMessage(null, msg);
		request.addCallbackParam("errorProcess", true);
		request.addCallbackParam("validationFailed", true);
	}

	public static void logFailed(FUNC_NAME func_name, String details) {
		logFailed(null, func_name, details);
	}

	public static void logFailed(String compID, String details, String summary) {
		FacesMessage msg = new FacesMessage(details, summary);
		msg.setSeverity(FacesMessage.SEVERITY_ERROR);

		if (StringUtils.hasText(compID)) {
			FacesContext.getCurrentInstance().addMessage(null, msg);
		} else {
			FacesContext.getCurrentInstance().addMessage(compID, msg);
		}
	}

	/**
	 * Insert a message to component ID
	 * 
	 * @param compID
	 * @param details
	 */
	public static void logFailed(String compID, String details) {
		logFailed(compID, details, "");
	}

	/*
	 * Insert message to wrapper component
	 */
	public static void logFailed(String details) {
		logFailed("", details, "");
	}
}
